package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.DiscountProductRechargeDAO;
import com.ecoolex.discount.sales.core.service.QuanMaoService;
import com.ecoolex.discount.sales.entity.*;
import com.ecoolex.discount.sales.entity.DiscountProductRecharge.Column;
import com.ecoolex.discount.sales.enums.DiscountProductServiceType;
import com.ecoolex.discount.sales.pojo.EditDiscountProductRechargeDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.discount.sales.entity.DiscountProductRecharge.NOT_DELETED;

@Service
public class DiscountProductRechargeManager {
    @Autowired
    private DiscountProductRechargeDAO dao;

    @Autowired
    private QuanMaoService quanMaoService;
    @Autowired
    private DiscountProductManager discountProductManager;

    void validityVerify(DiscountProduct product) {
        // 验证服务是否能支持
        if (DiscountProductServiceType.QUAN_MAO.equal(product.getServiceType())) {
            quanMaoService.validityVerify(product);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "充值折扣类型不知道此服务类型");
        }
    }

    void onlineVerify(DiscountProduct product) {
        BizAssert.isTrue(Check.notNull(product), "折扣产品不存在");
        DiscountProductRecharge recharge = getByProductId(product.getProductId());
        BizAssert.isTrue(Check.notNull(recharge), "折扣充值产品不存在");

        if (DiscountProductServiceType.QUAN_MAO.equal(product.getServiceType())) {
            quanMaoService.onlineVerify(product, recharge);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "充值折扣类型不支持此服务类型");
        }

    }

    void create(DiscountProduct product, String createUser) {
        DiscountProductRecharge record = new DiscountProductRecharge();
        record.setProductId(product.getProductId());
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    @Transactional
    public void update(Integer productId, EditDiscountProductRechargeDTO edit, String updateUser) {
        DiscountProductRecharge record = new DiscountProductRecharge();
        record.setAccountInputDescribe(edit.getAccountInputDescribe());
        record.setAccountInputPlaceholder(edit.getAccountInputPlaceholder());
        record.setProductId(productId);
        record.setCurrencyType(edit.getCurrencyType());
        record.setCounterPriceType(edit.getCounterPriceType());
        record.setMaxBuyNumber(edit.getMaxBuyNumber());
        record.setCounterPrice(edit.getCounterPrice());
        record.setRetailPrice(edit.getRetailPrice());
        record.setMinCounterPrice(edit.getMinCounterPrice());
        record.setMaxCounterPrice(edit.getMaxCounterPrice());
        record.setCounterPriceDiscountPercent(edit.getCounterPriceDiscountPercent());
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DiscountProductRechargeExample example = new DiscountProductRechargeExample();
        example.createCriteria()
                .andProductIdEqualTo(productId)
                .andDeletedEqualTo(DiscountProductGiftCard.NOT_DELETED);

        int row = dao.updateByExampleSelective(record,
                example,
                Column.accountInputDescribe,
                Column.accountInputPlaceholder,
                Column.currencyType,
                Column.counterPriceType,
                Column.counterPrice,
                Column.retailPrice,
                Column.maxBuyNumber,
                Column.minCounterPrice,
                Column.maxCounterPrice,
                Column.counterPriceDiscountPercent,
                Column.updateUser,
                Column.updateTime);

        BizAssert.isTrue(row == 1, "更新充值信息失败");
        // 如果上线则需要做参数检查
        discountProductManager.checkIfOnlineAndVerify(productId);
    }

    public DiscountProductRecharge getByProductId(Integer productId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(productId, NOT_DELETED);
    }

    public void processOrder(DiscountOrder order, String updateUser) {
        DiscountProductRecharge recharge = getByProductId(order.getProductId());
        BizAssert.isTrue(Check.notNull(recharge), "ProductId={}, 充值配置不存在", order.getProductId());

        if (DiscountProductServiceType.QUAN_MAO.equal(order.getServiceType())) {
            quanMaoService.processOrder(order, recharge, updateUser);
        } else {
            BizAssert.throwBizException(ResultCode.BIZ.build(1), "服务类型不支持");
        }
    }
}